package br.sales.controller;

import java.util.ArrayList;
import java.util.List;

import br.sales.entity.DynamicQuery;
import br.sales.entity.Newsletter;
import br.sales.entity.exception.EntityExistsException;
import br.sales.entity.exception.EntityNotExistsException;
import br.sales.persistence.IPersistence;
import br.sales.persistence.PersistenceFactory;
import br.sales.persistence.exception.DBContainerNotInitializedException;

public class NewsletterControlerImpl implements INewsletterControler{

	private static INewsletterControler newsletterControler;
	
	public NewsletterControlerImpl() {
	}
	
	public static INewsletterControler getInstance(){
		if (newsletterControler == null) {
			synchronized (INewsletterControler.class) {
				if (newsletterControler == null){
					newsletterControler = new NewsletterControlerImpl();
				}
			}
		}
		return newsletterControler;
	}
	
	@Override
	public Newsletter create(Newsletter newsletter)
			throws DBContainerNotInitializedException, EntityExistsException {
		
		IPersistence persistence = PersistenceFactory.createSessionDB4ODAO();
		try{
			Newsletter n = retrieve(newsletter.getEmail(),persistence);
			if (n == null){
				n = create(newsletter,persistence);
				n.setId(persistence.getObjectId(n));
				persistence.update(n);
				persistence.commit();
				return n;
			}else{
				throw new EntityExistsException();
			}
		}catch (DBContainerNotInitializedException e) {
			throw new DBContainerNotInitializedException();
		}
		
	}
	
	@Override
	public Newsletter update(Newsletter newsletter)
			throws Exception {
		IPersistence persistence = PersistenceFactory.createSessionDB4ODAO();
		
		Newsletter n = retrieve(newsletter.getEmail(),persistence);
		
		if (n == null || (n != null && persistence.getObjectId(n) == persistence.getObjectId(newsletter))){
			//n.copyFields(newsletter);
			n = update(newsletter, persistence);
			persistence.commit();
			return n;
		} else {
			throw new EntityExistsException();
		}
		
	}

	@Override
	public void delete(Newsletter newsletter)
			throws DBContainerNotInitializedException, EntityNotExistsException {
		IPersistence persistence = PersistenceFactory.createSessionDB4ODAO();
		try{
		Newsletter n = retrieve(newsletter.getEmail(),persistence);
		if (n != null){
			delete(n,persistence);
			persistence.commit();
		}else{
			throw new EntityNotExistsException();
		}
		}catch (DBContainerNotInitializedException e) {
			throw new DBContainerNotInitializedException();
		}
	}

	@Override
	public void delete(Newsletter newsletter, IPersistence persistence)
			throws DBContainerNotInitializedException {
		persistence.delete(newsletter);
	}

	@Override
	public Newsletter create(Newsletter newsletter, IPersistence persistence) throws DBContainerNotInitializedException {
		return (Newsletter) persistence.save(newsletter);
	}

	@Override
	public Newsletter update(Newsletter newsletter, IPersistence persistence)
			throws DBContainerNotInitializedException, EntityExistsException {
		return (Newsletter) persistence.update(newsletter);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Newsletter> listAll() throws DBContainerNotInitializedException {
		IPersistence persistence = PersistenceFactory.createSessionDB4ODAO();
		try{
			return persistence.retrieveAll(Newsletter.class);
		}catch (DBContainerNotInitializedException e) {
			throw new DBContainerNotInitializedException();
		}
	}

	
	@Override
	public List<Newsletter> listLike(String value) throws DBContainerNotInitializedException {

		IPersistence persistence = PersistenceFactory.createSessionDB4ODAO();
		try{
				List<DynamicQuery> dqs = new ArrayList<DynamicQuery>();
				dqs.add(new DynamicQuery(DynamicQuery.LIKE, "email", value));
				return persistence.createNativeQuery(dqs, Newsletter.class);
			
		}catch (DBContainerNotInitializedException e) {
			throw new DBContainerNotInitializedException();
		}		
	}

	@Override
	public Newsletter retrieve(String email)
			throws DBContainerNotInitializedException{
		try{
			IPersistence persistence = PersistenceFactory.createSessionDB4ODAO();
			return retrieve(email,persistence);
		}catch (DBContainerNotInitializedException e) {
			throw new DBContainerNotInitializedException();
		}
	}

	@Override
	public Newsletter retrieve(String email, IPersistence persistence)
			throws DBContainerNotInitializedException {
		return (Newsletter) persistence.retrieveByExample(new Newsletter(email));
	}

}
